﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Utils;
using PanasonicWarranty.DAL;
namespace PanasonicWarranty.Business
{
    public partial class SysUser
    {
    	#region public properties
		[Required(ErrorMessage="UserID Required")]
		public int UserID { get; set; }
		[Required(ErrorMessage="User Name Required")]
		public string UserName { get; set; }
		[Required(ErrorMessage="Password Required")]
		public string Password { get; set; }
		public string FullName { get; set; }
		public string Email { get; set; }
		public string Phone { get; set; }
		public string Address { get; set; }
		public int? AgencyID { get; set; }
		public DateTime? CreatedDate { get; set; }
		public DateTime? LastModifiedDate { get; set; }
		[Required(ErrorMessage="Disabled Required")]
		public bool Disabled { get; set; }
		#endregion

		#region constructor
		public SysUser(){ }

		public SysUser(IDataReader rd)
		{
			this.UserID = (int)rd["UserID"];
			this.UserName = rd["UserName"].ToString();
			this.Password = rd["Password"].ToString();
			this.FullName = rd["FullName"].ToString();
			this.Email = rd["Email"].ToString();
			this.Phone = rd["Phone"].ToString();
			this.Address = rd["Address"].ToString();
			this.AgencyID = rd["AgencyID"].Equals(DBNull.Value) ? (int?)null : (int)rd["AgencyID"];
			this.CreatedDate = rd["CreatedDate"].Equals(DBNull.Value) ? (DateTime?)null : (DateTime)rd["CreatedDate"];
			this.LastModifiedDate = rd["LastModifiedDate"].Equals(DBNull.Value) ? (DateTime?)null : (DateTime)rd["LastModifiedDate"];
			this.Disabled = (bool)rd["Disabled"];
		}
		#endregion

		#region private method
        private bool Create()
        {
            return DBSysUser.Create(UserName, Password, FullName, Email, Phone, Address, AgencyID, CreatedDate, LastModifiedDate, Disabled);
        }

        private bool Update()
        {
            return DBSysUser.Update(UserID, UserName, Password, FullName, Email, Phone, Address, AgencyID, CreatedDate, LastModifiedDate, Disabled);
        }
		#endregion

		#region public method
		public bool Save()
		{
            if(UserID != 0)
            {
                return Update();
            }
            else
            {
                return Create();
            }
		}
		#endregion

		#region static method
        public static bool Delete(int UserID)
        {
            return DBSysUser.Delete(UserID);
        }

        public static List<SysUser> GetAll()
        {
            return ObjectClass.CreateList<SysUser>(DBSysUser.GetAll());
        }

        public static SysUser GetByKey(int UserID)
        {
            return ObjectClass.CreateInstance<SysUser>(DBSysUser.GetByKey(UserID));
        }

        public static List<SysUser> GetByTop(string top, string where, string order)
        {
            return ObjectClass.CreateList<SysUser>(DBSysUser.GetByTop(top, where, order));
        }
		#endregion
	}
}
